<?php
App::uses('AppController', 'Controller');
/**
 * Estacoes Controller
 *
 * @property Estacao $Estacao
 * @property PaginatorComponent $Paginator
 */
class EstacoesController extends AppController {

	public $components = array('Paginator', 'Session');

/**
 * index method
 *
 * @return void
 */
	public function pesquisador_index() {
		$this->Estacao->recursive = 0;
		$this->set('estacoes', $this->Paginator->paginate());
		$this->set('parsers', Configure::read('Parsers'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function pesquisador_view($id = null) {
		if (!$this->Estacao->exists($id)) {
			throw new NotFoundException(__('Invalid estacao'));
		}
		$options = array('conditions' => array('Estacao.' . $this->Estacao->primaryKey => $id));
		$this->set('estacao', $this->Estacao->find('first', $options));
		$this->set('parsers', Configure::read('Parsers'));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Estacao->create();
			if ($this->Estacao->save($this->request->data)) {
				$this->Session->setFlash(__('The estacao has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The estacao could not be saved. Please, try again.'));
			}
		}
		else {
			$this->set('parsers', Configure::read('Parsers'));
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Estacao->exists($id)) {
			throw new NotFoundException(__('Invalid estacao'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Estacao->save($this->request->data)) {
				$this->Session->setFlash(__('The estacao has been saved.'));
				return $this->redirect(array('action' => 'index', 'pesquisador' => true));
			} else {
				$this->Session->setFlash(__('The estacao could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Estacao.' . $this->Estacao->primaryKey => $id));
			$this->request->data = $this->Estacao->find('first', $options);
		}

		$this->set('parsers', Configure::read('Parsers'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Estacao->id = $id;
		if (!$this->Estacao->exists()) {
			throw new NotFoundException(__('Invalid estacao'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Estacao->delete()) {
			$this->Session->setFlash(__('The estacao has been deleted.'));
		} else {
			$this->Session->setFlash(__('The estacao could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index', 'pesquisador' => true));
	}

	public function pesquisador_leitura($id) {
		$estacao = $this->Estacao->findById($id);

        $parsers = Configure::read('Parsers');
        $tipo = $estacao['Estacao']['tipo'];
    	if ($parsers[$tipo] == 'HS01')
    		$this->redirect(array('controller' => 'leiturasHs01', 'action' => 'index', 'pesquisador' => true, $id));
    	else if ($parsers[$tipo] == 'HS02')
    		$this->redirect(array('controller' => 'leiturasHs02', 'action' => 'index', 'pesquisador' => true, $id));
        else if ($parsers[$tipo] == 'M02')
            $this->redirect(array('controller' => 'leiturasM02', 'action' => 'index', 'pesquisador' => true, $id));
        else
        	$this->redirect('/');
	}
}
